// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.

// Author: Shuo Chen (chenshuo at chenshuo dot com)
//
// This is an internal header file, you should not include this.

#ifndef MUDUO_NET_ACCEPTOR_H
#define MUDUO_NET_ACCEPTOR_H

#include <boost/function.hpp>
#include <boost/noncopyable.hpp>

#include <muduo/net/Channel.h>
#include <muduo/net/Socket.h>

namespace muduo
{
  namespace net
  {

    class EventLoop;

    class InetAddress;

///
/// Acceptor of incoming TCP connections.
///
    class Acceptor : boost::noncopyable
    {
    public:
        typedef boost::function<void(int sockfd, const InetAddress &)> NewConnectionCallback;

        Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool reuseport);

        ~Acceptor();

        void setNewConnectionCallback(const NewConnectionCallback &cb)
        {
            newConnectionCallback_ = cb;
        }

        bool listenning() const
        {
            return listenning_;
        }

        void listen();

    private:
        //核心工作是由NewConnectionCallback函数完成
        void handleRead();
        //运行Acceptor事件的eventloop，它也是muduo中必需的一个eventloop(因为eventloop线程池可能为空)
        EventLoop *loop_;
        Socket acceptSocket_; //是产生事件的socket文件描述符
        //因为事件不仅包含文件描述符，还包含对应这个事件的处理函数因此需要把文件描述符和这些处理函数封装在一起，Channel结构体就是事件封装结构体
        Channel acceptChannel_;
        //由用户通过调用setNewConnectionCallback函数设置。这里的Acceptor用户一般是Tcpserver
        NewConnectionCallback newConnectionCallback_;
        bool listenning_;
        int idleFd_;
    };

  }
}

#endif  // MUDUO_NET_ACCEPTOR_H
